<?php
/**
 * 配置文件临时设置函数库
 * 
 * $Id: set.functions.php 122 2010-05-28 10:05:16Z Administrator $
 * $Revision: 122 $
 * $Author: Administrator $
 * $Date: 2010-05-28 18:05:16 +0800 (星期五, 28 五月 2010) $
 * 当你运行一个脚本时而系统原来的设置不满足当前的要求，就可以使用本文件进行重新设置
 * 
 * 
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage functions
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	if (__DEBUG__) {
		die ( sprintf ( 'Forbidden 403 at file %s on line %d', __FILE__, __LINE__ ) );
	} else {
		die ( 'Forbidden 403' );
	}
}
/**
 * 设置页面CSS,JS,RSS,FETCH,INCLUDE代码
 *
 * @param Array $Rule 要设置的代码信息比如CSS　
 * setRule(array('src'=>"./view/{$_COOKIE['SKIN']}/css/login.css"),'css','a');
 * @param String $type 类型有 JS,CSS,RSS,FETCH,INCLUDE
 * @param String $method 操作类型 A(结尾),I(前面),R(取代),C(清除)
 * @return true
 */
function setRule($Rule, $type = "js", $method = "A") {
	global $__CSS, $__JS, $__RSS, $__FETCH, $__INCLUDE;
	$tmp = array ();
	$tmp ['JS'] = $__JS;
	$tmp ['CSS'] = $__CSS;
	$tmp ['RSS'] = $__RSS;
	$tmp ['FETCH'] = $__FETCH;
	$tmp ['INCLUDE'] = $__INCLUDE;
	$type = strtoupper ( $type );
	if (! is_array ( $tmp [$type] )) {
		$tmp [$type] = array ();
	}
	switch (strtolower ( $method )) {
		case "a" :
			array_push ( $tmp [$type], $Rule );
			break;
		case "i" :
			array_unshift ( $tmp [$type], $Rule );
			break;
		case "r" :
			while ( array_pop ( $tmp [$type] ) )
				;
			array_push ( $tmp [$type], $Rule );
		case "c" :
			while ( array_pop ( $tmp [$type] ) )
				;
			break;
		default :
	}
	$__JS = $tmp ['JS'];
	$__CSS = $tmp ['CSS'];
	$__RSS = $tmp ['RSS'];
	$__FETCH = $tmp ['FETCH'];
	$__INCLUDE = $tmp ['INCLUDE'];
	return true;
}
/**
 * 站点变量sitecfg设置
 *
 * @param String $key 名称
 * @param mixed $value 值
 */
function setSitecfg($key, $value = null) {
	global $sitecfg, $flibcfg;
	$value = $value !== null ? $value : (isset ( $sitecfg [$key] ) ? $sitecfg [$key] : (isset ( $flibcfg [$key] ) ? $flibcfg [$key] : null));
	if ($key === strtolower ( "dtd" )) {
		if (! in_array ( $value, array ("2", "3.2", "4.0s", "4.0t", "4.0f", "4.01s", "4.01t", "4.01f" ) )) {
			setSitecfg ( 'tagclose', '/' ); //自动关闭HTML标签
		} else {
			setSitecfg ( 'tagclose', '' );
		}
		return $sitecfg [$key] = $flibcfg [$key] = strtolower ( $value );
	}
	if ($key === strtolower ( "timezone" )) { //时区
		timezoneSet ( $value );
		return $sitecfg [$key] = $flibcfg [$key] = $value;
	}
	if ($key === "rewriteRule" && defined ( 'SID' ) && SID != '') {
		$value = '';
	}
	$sitecfg [$key] = $flibcfg [$key] = $value;
	return true;
}
/**
 * 设置分类时所使用的变量
 *
 * @param String $key 名称
 * @param mixed $value 值
 * @return true
 */
function setPagecfg($key = null, $value = null) {
	global $sitecfg;
	if (! $key or ! isset ( $value )) {
		setSitecfg ( "pagecfg" );
		return false;
	} else {
		$sitecfg ['pagecfg'] [$key] = $value;
		setSitecfg ( 'pagecfg', $sitecfg ['pagecfg'] );
		return true;
	}
}
/**
 * 根据GET变量，flibcfg,默认值更新SESSION,sitecfg,flibcfg
 *
 * @param string $getName 要设置的SESSION,sitecfg,flibcfg名称从GET中得来
 * @param string $defValue 如果sitecfg,flibcfg且GET变量为空时取这个值为session的值
 * @param string $regexp 过滤符合规则的字符
 * @param string $type 设置到COOKIE或session
 * @param bool $update 是否更新到flibcfg,sitecfg中
 */
function setGetValue($getName, $defValue = null, $regexp = '/[^a-zA-Z0-9_]+/', $type = 'cookie', $update = false) {
	global $flibcfg;
	$get = isset ( $_GET [$getName] ) ? trim ( $_GET [$getName] ) : null;
	$get = preg_replace ( $regexp, '', $get );
	$type = strtolower ( $type );
	switch ($type) {
		case 'session' :
			if (isset ( $_GET [$getName] )) {
				$_SESSION [$getName] = $get ? $get : null;
			} elseif (! isset ( $_SESSION [$getName] )) {
				$_SESSION [$getName] = isset ( $flibcfg [$getName] ) ? $flibcfg [$getName] : $defValue;
			}
			if ($update) {
				setSitecfg ( $getName, isset ( $_SESSION [$getName] ) ? $_SESSION [$getName] : null );
			}
			break;
		case 'cookie' :
			if (isset ( $_GET [$getName] )) {
				setHeader ( 'cookie', $getName, array ($get, time () + 86400 * 30 ) );
				$_COOKIE [$getName] = $get;
			} elseif (! isset ( $_COOKIE [$getName] )) {
				setHeader ( 'cookie', $getName, array (isset ( $flibcfg [$getName] ) ? $flibcfg [$getName] : $defValue, time () + 86400 * 30 ) );
				$_COOKIE [$getName] = isset ( $flibcfg [$getName] ) ? $flibcfg [$getName] : $defValue;
			}
			if ($update) {
				setSitecfg ( $getName, isset ( $_COOKIE [$getName] ) ? $_COOKIE [$getName] : null );
			}
			break;
		default :
	}
}
/**
 * 设置文档标题
 *
 * @param String $title 标题
 * @param String $method 方法（A）原标题后面（I）原标题前，（R）取代原标题
 * @param String $separator 标题之间的分隔号
 */
function setDocTitle($title, $method = "R", $separator = null) {
	global $sitecfg;
	if ($separator === null) {
		setSitecfg ( 'separator' );
		$separator = $sitecfg ['separator'];
	}
	if (! isset ( $title ) or ! $title) {
		$title = 'NoName';
	}
	if (isset ( $sitecfg ['siteName'] ) && $sitecfg ['siteName']) {
		$method = strtoupper ( $method );
		if ($method == 'A') {
			return setSitecfg ( 'DocTitle', $sitecfg ['siteName'] . $separator . $title );
		} elseif ($method == 'I') {
			return setSitecfg ( 'DocTitle', $title . $separator . $sitecfg ['siteName'] );
		}
	}
	return setSitecfg ( 'DocTitle', $title );
}
/**
 * 设置httpheader信息
 * 
 * 本函数主要用于设置HTML header信息。
 * 本函数可以发送wml,wbmp,wmlc,下载，重定向，定时刷新网页等HTML头.
 *
 * @param String $type 类型,支持WAP开发，可以为wml,wbmp,wmlc,wmls,wmlsc,xsl,download,location,refresh和其它自定义
 * @param String $value 值,只有当值为xsl,download时才会有，他为文件名。
 * @param String $value2 值,当$type为refresh时$value2为刷新时间。
 */
function setHeader($type, $value = null, $value2 = null) {
	global $flibcfg;
	switch (strtolower ( $type )) {
		case "wml" :
			if ($value === NULL) {
				header ( "Content-type: text/vnd.wap.wml;" );
			} else {
				header ( "Content-type: text/vnd.wap.wml; charset=" . $value );
			}
			break;
		case 'cache' :
			if ($value !== null) {
				if ($value <= 0) {
					//缓存失效
					//HTTP1.1
					header ( "Cache-Control: no-cache, must-revalidate" );
					//HTTP1.0
					header ( "Pragma: no-cache" );
				} else {
					header ( "Cache-Control: max-age=" . intval ( $value ) );
					header ( "Expires: " . gmdate ( "D, d M Y H:i:s", intval ( $value ) + time () ) . " GMT" );
				}
			}
			break;
		case 'cookie' :
			if (is_string ( $value )) {
				if (is_string ( $value2 )) {
					setcookie ( $flibcfg ['cookie_prefix'] . '_' . $value, $value2 );
				} elseif (is_array ( $value2 )) {
					array_unshift ( $value2, $flibcfg ['cookie_prefix'] . '_' . $value );
					call_user_func_array ( 'setcookie', $value2 );
				}
			}
			break;
		case 'p3p' :
			header ( 'P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"' );
			break;
		case "wbmp" :
			header ( "Content-type: image/vnd.wap.wbmp" );
			break;
		case "wmlc" :
			header ( "Content-type: application/vnd.wap.wmlc" );
			break;
		case "wmls" :
			header ( "Content-type: text/vnd.wap.wmls" );
			break;
			break;
		case "wmlsc" :
			header ( "Content-type: application/vnd.wap.wmlsc" );
			break;
		case "xls" :
			header ( "Content-type: application/x-msexcel" );
			header ( "Content-Disposition: attachment; filename={$value}.xls" );
			break;
		case "download" :
			header ( "Content-type: application/octet-stream" );
			header ( "Content-Disposition: attachment; filename={$value}" );
			break;
		case 'location' :
			header ( 'Location: ' . $value );
			break;
		case 'http' :
			header ( "HTTP/1.0 " . $value );
			if (! class_exists ( 'fasttpl' )) {
				loadClass ( 'fasttpl', __FILE__, __LINE__ );
			}
			$tpl = new fasttpl ();
			setDocTitle ( 'HTTP/' . $value );
			if ($value2 === null) {
				switch (array_shift ( explode ( ' ', $value ) )) {
					case '404' :
						$value2 = 'The URL <a href="' . strtr ( $_SERVER ['REQUEST_URI'], array ('&' => '&amp;' ) ) . '">' . strtr ( $_SERVER ['REQUEST_URI'], array ('&' => '&amp;' ) ) . '</a> was not found on this server.<br /> The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.<br /><br />Open <a href="/">' . $flibcfg ['HOST'] . '</a> home page.<br />';
						break;
					case '403' :
						if (! __DEBUG__) {
							$value2 = "This module disabled";
						} else {
							$value2 = 'You don\'t have permission to access <a href="' . strtr ( $_SERVER ['REQUEST_URI'], array ('&' => '&amp;' ) ) . '">' . strtr ( $_SERVER ['REQUEST_URI'], array ('&' => '&amp;' ) ) . '</a> on this server.<br /><br />Open <a href="/">' . $flibcfg ['HOST'] . '</a> home page.';
						}
						break;
					default :
						$value2 = $value;
				}
			}
			$tpl->assign ( 'http', array ('status' => $value, 'content' => $value2 ) );
			setRule ( null, 'CSS', 'C' );
			setRule ( null, 'JS', 'C' );
			if (isset ( $flibcfg ['http'] ['tpl'] ) && is_string ( $flibcfg ['http'] ['tpl'] ) && file_exists ( getSiteCurrent ( true ) . $flibcfg ['http'] ['tpl'] )) {
				$mytpl = getSiteCurrent ( true ) . $flibcfg ['http'] ['tpl'];
				if (isset ( $flibcfg ['http'] ['css'] ) && is_array ( $flibcfg ['http'] ['css'] )) {
					foreach ( $flibcfg ['http'] ['css'] as $v ) {
						$v ['src'] = getSiteCurrent ( false ) . $v ['src'];
						setRule ( $v, 'css', 'a' );
					}
				}
				if (isset ( $flibcfg ['http'] ['js'] ) && is_array ( $flibcfg ['http'] ['js'] )) {
					foreach ( $flibcfg ['http'] ['js'] as $v ) {
						$v ['src'] = getSiteCurrent ( false ) . $v ['src'];
						setRule ( $v, 'js', 'a' );
					}
				}
			} else {
				$mytpl = __FASTTPL__ . 'http.fs';
				setSitecfg ( 'dtd', '4.0t' );
			}
			$tpl->display ( $mytpl );
			exit ();
			break;
		case 'refresh' :
			header ( 'refresh: ' . $value2 . '; url=' . $value );
			break;
		default :
			header ( "{$type}: {$value}" );
	}
}
/**
 * 返回本模块信息
 *
 * @return string
 */
function setprintInfo() {
	$version = '$Revision: 122 $';
	$moudle = explode ( "functions", __FILE__ );
	$moudle = $moudle [1] ? substr ( $moudle [1], 1 ) . 'functions' : $moudle;
	$return = '';
	$return .= startTable ();
	$return .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . $moudle . '</center>', 'colspan' => '2' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'author' ), array ('class' => 'v', 'content' => 'glzaboy' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'version' ), array ('class' => 'v', 'content' => $version ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'HTTP header support' ), array ('class' => 'v', 'content' => 'yes' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'setSitecfg' ), array ('class' => 'v', 'content' => 'yes' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'DocTitle' ), array ('class' => 'v', 'content' => 'yes' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'pagecfg' ), array ('class' => 'v', 'content' => 'yes' ) ) );
	$return .= printRow ( array (array ('class' => 'e', 'content' => 'rule support' ), array ('class' => 'v', 'content' => 'js,css,include,fetchr,rss,http' ) ) );
	$return .= endTable ();
	return $return;
}